<?php
/**
 * compound
 * 化合物
 * @author Administrator
 * @version
 */
class Model_Tables_Compound extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'compound';
    /**
     * add
     * 增加化合物
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'casno' => $params['casno'],
		'compoundno' => $params['compoundno'],
		'compoundname' => $params['compoundname'],
		'compoundnameen' => $params['compoundnameen'],
		'smile' => $params['smile'],
        'formula' => $this->GetFormula($params['smile']),//$params['formula'],
		'molecularweight' => $this->GetWeight($params['smile']),//$params['molecularweight'],
        'product' => $params['product'],
		'patent' => $params['patent'],
		'appearance' => $params['appearance'],
		'meltingpoint' => $params['meltingpoint'],
		'boilingpoint' => $params['boilingpoint'],
		'solubility' => $params['solubility'],
		'stability' => $params['stability'],
		'projectid' => $params['projectid'],
		'synthesisroute' => $params['synthesisroute'],
		'synthesisdoc' => $params['synthesisdoc'],
        );
        return $this->insert($data) && $this->UpdateNewSerialized();
    }
    /**
     * modify
     * 编辑化合物
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'casno' => $params['casno'],
		'compoundno' => $params['compoundno'],
		'compoundname' => $params['compoundname'],
		'compoundnameen' => $params['compoundnameen'],
		'smile' => $this->GetSmile($params['smile']),//$params['smile'],
		'formula' => $this->GetFormula($params['smile']),//$params['formula'],
		'molecularweight' => $this->GetWeight($params['smile']),//$params['molecularweight'],
		'product' => $params['product'],
		'patent' => $params['patent'],
		'appearance' => $params['appearance'],
		'meltingpoint' => $params['meltingpoint'],
		'boilingpoint' => $params['boilingpoint'],
		'solubility' => $params['solubility'],
		'stability' => $params['stability'],
		'projectid' => $params['projectid'],
		'synthesisroute' => $params['synthesisroute'],
		'synthesisdoc' => $params['synthesisdoc'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where ) && $this->UpdateSerialized($id);
    }
    /**
     * modifyAjax
     * 无刷新编辑化合物
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转化合物
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update compound set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除化合物
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取化合物信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function GetSmile($smile){
    	$db = $this->getAdapter();
    	$sql = "select molecule_to_smiles(smiles_to_molecule('$smile'))";
    	return $db->fetchOne($sql);
    }
    public function GetFormula($smile){
    	$db = $this->getAdapter();
    	$sql = "select molformula(smiles_to_molecule('$smile'))";
    	return $db->fetchOne($sql);
    }
	public function GetWeight($smile){
    	$db = $this->getAdapter();
    	$sql = "select molweight(smiles_to_molecule('$smile'))";
    	return $db->fetchOne($sql);
	}
	public function UpdateSerialized($id){
		$db = $this->getAdapter();
    	$sql = "update compound set compoundserialized = MOLECULE_TO_SERIALIZEDOBMOL(SMILES_TO_MOLECULE(smile)) where id='$id'";
    	return $db->query($sql);
	}
	public function UpdateNewSerialized(){
		$db = $this->getAdapter();
    	$sql = "update compound set compoundserialized = MOLECULE_TO_SERIALIZEDOBMOL(SMILES_TO_MOLECULE(smile)) order by id desc limit 1";
    	return $db->query($sql);
	}
    /**
     * getCount
     * 获取结构式数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			//$where = $db->quoteInto("id=?", $s);
			//$where = $db->quoteInto("MATCH_SUBSTRUCT(?,MOLECULE_TO_SERIALIZEDOBMOL(SMILES_TO_MOLECULE(smile)))", $s);
			//$where = $db->quoteInto("MATCH_SUBSTRUCT(molecule_to_smiles(smiles_to_molecule(?)),MOLECULE_TO_SERIALIZEDOBMOL(SMILES_TO_MOLECULE(smile)))", $s);
			$where = $db->quoteInto("MATCH_SUBSTRUCT(molecule_to_smiles(smiles_to_molecule(?)),compoundserialized)", $s);
    		//$where .= $db->quoteInto(" or smile like ? ","%$s%");
			$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取结构式数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("MATCH_SUBSTRUCT(molecule_to_smiles(smiles_to_molecule(?)),compoundserialized)", $s);
			//$where .= $db->quoteInto(" or smile like ? ","%$s%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
}